﻿using Ec.NETCoreFramework.Common;
using Ec.NETCoreFramework.Common.Clients;
using Ec.NETCoreFramework.Common.Core;
using Ec.NETCoreFramework.Common.Core.Helpers;
using Ec.NETCoreFramework.Common.Plug.Core;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace MQ_Approval
{
    /// <summary>
    /// 汇报同步基类
    /// </summary>
    public class ApprovalSync
    {
        protected ConsumeConfig _consumeConfig;
        private RequestAppSecret _appSecret;
        private string _SoftwareVersion = string.Empty;

        public ApprovalSync(RequestAppSecret appSecret, ConsumeConfig consumeConfig)
        {
            _appSecret = appSecret;
            _SoftwareVersion = AppsettingsHelper.app(new string[] { "SoftwareVersion" });
            _consumeConfig = consumeConfig;

        }
        /// <summary>
        /// 执行入口
        /// </summary>
        /// <param name="dic"></param>
        /// <returns></returns>
        public virtual Task Excute(Dictionary<string, object> dic)
        {
            return Task.CompletedTask;
        }
        /// <summary>
        /// 获得汇报列表
        /// </summary>
        /// <param name="item"></param>
        /// <param name="cursor"></param>
        /// <returns></returns>
        public async Task GetApprovalList(ApprovalOutput item, long begin, long end, string cursor)
        {

            var dic = new Dictionary<string, object>()
            {
                {"fk_id",item.fk_id },
                {"db_id",item.db_id },
                {"template_id",item.suite_template_id },
                {"start_time",begin },
                {"end_time",end },
                {"cursor",cursor }
            };
            var result = await new ApiRequestClient(_appSecret).
            GetResponse<JObject>(_consumeConfig.WechatDomain,
            string.Empty, dic, "/wx/approval/list", new Dictionary<string, string>() { { "api-version", "2.0" }, { "version", _SoftwareVersion } }, 10000);

            if (!result.Success) return;

            var re = ((IResponse<JObject>)result).Content;

            var Approvaluuid_list = re["sp_no_list"] as JArray;

            foreach (string o in Approvaluuid_list)
            {
                item.Approvaluuid_list.Add(o);
            }

            if (!re.ContainsKey("cursor")) return;

            cursor = re["cursor"].ToString();

            if (string.IsNullOrEmpty(cursor)) return;

            await GetApprovalList(item, begin, end, cursor);
        }
        /// <summary>
        /// 汇报详情
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public async Task GetApprovalDetail(ApprovalOutput ApprovalOutput)
        {
            var j = 10;
            for (var i = 0; i < ApprovalOutput.Approvaluuid_list.Count; i += 10)
            {
                var cList = ApprovalOutput.Approvaluuid_list.Take(j).Skip(i).ToList();
                j += 10;
                var uuids = string.Join("|", cList.Select(it => { return it; }));
                var item = ApprovalOutput.Approvaluuid_list[i];
                var dic = new Dictionary<string, object>()
            {
                {"fk_id",ApprovalOutput.fk_id },
                {"db_id",ApprovalOutput.db_id },
                {"sp_no",HttpUtility.UrlEncode(uuids) },
            };
                var result = await new ApiRequestClient(_appSecret).
                GetResponse<JArray>(_consumeConfig.WechatDomain,
                string.Empty, dic, "/wx/approval/detail", new Dictionary<string, string>() { { "api-version", "2.0" }, { "version", _SoftwareVersion } }, 10000);
                if (!result.Success) throw new Exception(result.Message);

                await Push(result, ApprovalOutput);
            }

        }

        /// <summary>
        /// 推数据
        /// </summary>
        /// <param name="mediaOutputs"></param>
        /// <returns></returns>
        public async Task Push(IResponse arr, ApprovalOutput item)
        {
            var dic = new Dictionary<string, object>()
                {
                    {"fk_id",item.fk_id },
                    {"content",$"<xml><![CDATA[{Json.ToJson(arr)}]]></xml>" }
                };
            var result = await new ApiRequestClient(_appSecret).
        PostBinary<JObject>(_consumeConfig.Domain,
        "wxgroup.oa.approval.data.sync", dic);
            if (!result.Success)
            {
                throw new Exception(result.Message);
            }

        }
    }
}
